// 引入 Vue 3 和 Vue Router
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'

// 引入组件
import PreLogin from "../views/login/PreLogin.vue"
import PlcLogin from "../views/login/PlcLogin.vue"
import PlcRegister from "../views/login/PlcRegister.vue"
import PlcResetPsw from "../views/login/PlcResetPsw.vue"
import PlcHome from "../views/home/Home.vue"
import PlcFunction from "../views/function/PlcFunction.vue"

// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',// 路径
            component: PreLogin// 跳转到的组件
        },
        {
            path: '/',// 路径
            redirect: '/prelogin'// 重定向到的路径
        },
        {
            path: '/plchome', // 路径
            component: PlcHome // 跳转到的组件
        },
        {
            path: '/prelogin',
            component: PreLogin
        },
        {
            path: '/plclogin',
            component: PlcLogin
        },
        {
            path: '/plcregister',
            component: PlcRegister
        },
        {
            path: '/plcresetpsw',
            component: PlcResetPsw
        },
        {
            path: '/plcfunction',
            component: PlcFunction
        },
    ]
})

export default router

// 导航守卫，前置处理
router.beforeEach((to, from, next) => {
    let isAuthenticated = !!sessionStorage.getItem('userInfo')
    // 如果路由要跳转到除了登录和注册的界面的话就判断是否已经登录，如果没有登录就强制跳到登录界面
    if (to.path !== '/prelogin' && to.path !== '/plclogin' && to.path !== '/plcregister'  && !isAuthenticated ) {
        next({ path: '/prelogin' })
        // 这里 Message 提示需要根据你实际的情况调整
        Message({
            message: '请先登录！',
            type: 'warning',
            grouping: true,
        });
    } else {
        //sessionStorage.clear();
        next()
    }
})